- C INTRODUCTION
- C INDEX
- HISTORY & FEATURES OF C
- WHY LEARN C
- SETTING UP C ENVIRONMENT
- WHAT IS A PROGRAM
- WRITING FIRST C PROGRAM
- C PROGAMMING CODE COMPILATION AND EXECUTION
- RECEIVING INPUT IN C
- C INTRO PRACTICE
- BASIC SYNTAX AND DATA TYPES
- BASICS OF PROGRAMMING
- WHAT IS PROGRAMMING
- WHY WE NEED TO LEARN PROGRAMING
- BENEFITS OF LEARNING PROGRAMING
- SOME EXAMPLES OF PROGRAMING
- EXAMPLE OF BASIC CODING IN C PROGRAMING
- HISTORY OF PROGRAMING
- PROGRAMMING LANGUAGE
- WHAT IS CHAT GPT
- WHAT IS MACHINE LEARNING
- WHAT IS IOT
- EVOLUTION OF C PROGRAMMING LANGUAGE
- WHAT IS FORTAN
- WHAT IS PL/I
- AT&T & BELL LABS
- ABOUT DENIS RITCHIE
- C INSTALLATION
- DOWNLOAD AND INSTALL C/GCC COMPILER FOR WINDOWS
- SOURCE CODE EDITORS
- INSTALLING C/GCC COMPILER FOR WINDOWS C/GCC COMPILER
- SETTING UP PATH VARIABLE
- C PROGRAMMING STRUCTURE
- COMMENTS
- DATA TYPES AND VARIABLES
- DATA TYPES
- VARIABLE
- CONSTANTS AND VARIABLE
- HOW TO CREATE A VARIABLE
- FORMAT SPECIFIER
- MEMORY
- PRACTICE QUESTION
- EXERCISE
- C KEYWORDS
- C OPERATORS
- ARITHMETIC OPERATORS
- RELATIONAL OPERATORS
- LOGICAL OPERATORS
- BITWISE OPERATORS
- ASSIGNMENT OPERATORS
- INCREMENT AND DECREMENT OPERATORS
- CONDITIONAL (TERNARY) OPERATOR
- EXAMPLES OF OPERATORS
- MCQ PRACTICE QUESTION
- CONTROL STATEMENT
- CONDITIONAL STATEMENT
- IF STATEMENT
- NESTED IF
- NESTED IF ELSE
- IF ELSE IF ELSE IF
- SWITCH CASE
- EXAMPLES OF CONTROL STATEMENT
- PRACTICE QUESTIONS
- EXERCISE
- LOOPS
- TYPES OF LOOP
- WHILE LOOP
- INCREMENT OPERATOR
- FOR LOOP
- NESTED FOR LOOP
- BREAK AND CONTINUE
- DO WHILE LOOP
- GO TO
- EXAMPLES OF LOOP
- MCQ PRACTICE QUESTIONS
- EXERCISE
- CONSTANTS IN C
- FUNCTIONS
- WHAT IS FUNCTION
- PASSING VALUES B/W FUNCTION
- SCOPE OF FUNCTION
- CALL BY VALUE AND CALL BY REFERENCE
- MEMORY ADDRESS C
- CONCLUSION
- RECURSION
- RECURSION AND STACK
- ADDING FUNCTION TO THE LIBRARY
- INTEGER SIGNED AND UNSIGNED
- EXAMPLES OF FUNCTION
- EXERCISE
- MCQ PRACTICE QUESTIONS
- STORAGE CLASS IN C
- TYPES OF STORAGE CLASS
- AUTOMATIC STORAGE CLASS
- REGISTER STORAGE CLASS
- STATIC STORAGE CLASS
- EXTERNAL STORAGE CLASS
- C PREPROCESSOR
- ARRAYS
- ABOUT ARRAY
- ACCESS THE ELEMENT OF ARRAY
- REPLACING A VALUE OF AN ARRAY
- ARRAY DECLARATION
- ARRAY INITIALIZATION
- TWO DIMENSIONAL ARRAYS
- CHANGING ELEMENTS IN TWO DIMENSION ARRAY
- EXAMPLES OF ARRAY
- MCQ PRACTICE QUESTIONS
- EXERCISE
- STRING
- ABOUT STRING
- SPACE ALLOCATION OF A STRING IN C
- STANDARD LIBRARY A STRING FUNCTION
- EXAMPLES OF STRING
- MCQ PRACTICE QUESTIONS
- EXERCISE
- STRUCTURE
- ABOUT STRUCTURE
- HOW ELEMENT STORE IN STRUCTURE
- USING STRING IN STRUCTURE
- COPY STRUCTURE
- POINTER IN STRUCTURE
- MCQ PRACTICE QUESTIONS
- EXERCISE
- CONSOLE INPUT OUTPUT
- FILE INPUT OUTPUT
- C PROGRAMMING MCQ
- CODES IN C
- SIMPLE C PROGRAM
- C PROGRAM ON NUMBER
- C PROGRAM ON DATE TIME AND YEAR
- FACTORIAL AND FIBONACCI PROGRAM IN C
- PATTERN PROGRAM IN C
- C PROGRAM ON DATA TYPE AND UNION
- C PROGRAM ON MATH FUNCTION
- C PROGRAM ON PROGRESSION SERIES
- C PROGRAM ON AREA AND VOLUME
- C PROGRAM ON GCD LCM AND HCF
- C PROGRAM ON ARRAY
- C PROGRAM ON MATRIX
- C PROGRAM ON BITWISE OPERATION
- C PROGRAM ON STRING
- C PROGRAM ON FILE HANDLING
RECEIVING INPUT IN C
There are various ways for recieving input in C program.
For now we will check only one way that is going to be used in our program to recieve any use input.
The scanf()
function is commonly used to receive input from the user. It reads data from the standard input (usually the keyboard) and stores it in variables. For example:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
scanf()
is a function in C programming that allows you to read input from the user or from a file. It stands for "scan formatted" and is a part of the standard input/output library (stdio.h
).
Here's how scanf()
works in detail:
- Syntax: The general syntax of the
scanf()
function is:
scanf(format, &variable);
format
: It specifies the format in which the data is expected to be entered. For example,%d
is used for integers,%f
for floating-point numbers,%c
for characters,%s
for strings, etc.&variable
: It is the memory address of the variable where the input data will be stored. The&
(address-of) operator is used to get the address of the variable.
-
Reading Input: The
scanf()
function waits for the user to input data and press Enter. It then tries to match the entered data with the specified format. If the input matches the format, it is stored in the corresponding variable(s); otherwise, it can cause unexpected behavior. -
Ignoring Whitespace: By default,
scanf()
skips leading whitespace (spaces, tabs, newline) characters before reading input. However, it stops reading when it encounters the first whitespace character, so it might not be suitable for reading strings containing spaces. -
Return Value: The
scanf()
function returns the number of input items successfully matched and assigned. This can be useful for error checking. -
Handling Newlines: After entering data and pressing Enter, a newline character (
\n
) is also added to the input buffer. This can cause issues when usingscanf()
along with other input functions. To handle this, you can include a space before the format specifier, likescanf(" %c", &ch);
. This space will skip any leading whitespace characters, including newlines. -
Limitations: While
scanf()
is a convenient way to read input, it has limitations, especially when reading strings. It doesn't handle input that exceeds the size of the provided buffer, leading to buffer overflow vulnerabilities. For safer input handling, consider usingfgets()
.